import { ref } from 'vue';
let toast : any = ref({

});
let notify : any = ref({

});
let message : any = ref({});

export default function useWdToast() {
	const init = (key : any, { appToast, appNotify, appMessage }) => {
		toast.value[key] = appToast;
		notify.value[key] = appNotify
		message.value[key] = appMessage
		// if (!toast.value[key]) {
		// 	toast.value[key] = appToast;
		// }
		// if (!toast.value[key]) {
		// 	notify.value[key] = appNotify
		// }
	}
	const show = (msg : string) => {
		const page = getCurrentPages();

		let t = toast.value[page[page.length - 1].route ?? ""];
		if (t) {
			t.show(msg)
		}
	}

	const error = (msg : string) => {
		const page = getCurrentPages();
		let t = toast.value[page[page.length - 1].route ?? ""];
		if (t) {
			t.error(msg)
		}

	}

	const showNotify = (type : any, message : any) => {
		const page = getCurrentPages();
		let n = notify.value[page[page.length - 1].route ?? ""];
		if (n) {
			n.showNotify({
				type,
				message,
				zIndex: "999999999"
			});
		}
	}
	const loading = (msg) => {
		const page = getCurrentPages();
		let t = toast.value[page[page.length - 1].route ?? ""];
		if (t) {
			t.loading(msg)
		}
	}
	const close = () => {
		const page = getCurrentPages();
		let t = toast.value[page[page.length - 1].route ?? ""];
		if (t) {
			t.close()
		}
	}
	const success = (msg : string) => {
		const page = getCurrentPages();
		let t = toast.value[page[page.length - 1].route ?? ""];
		if (t) {
			t.success(msg)
		}
	}
	const confirm = (msg : string, title : string) => {
		const page = getCurrentPages();
		let t = message.value[page[page.length - 1].route ?? ""];
		if (t) {
			return t.confirm({
				msg: msg,
				title: title ?? "提示"
			})
		}
		return new Promise((resolve, reject) => {
			reject(new Error('MessageBox.confirm is not defined'));
		});
	}

	return {
		init,
		show,
		error,
		success,
		loading,
		close,
		confirm,
		showNotify,
	}
}